var calendar;
var current_days = [],sign_map = {};
$(document).ready(function(){
  
  // $(".flatpickr-next-month").click(function(){
  //   console.log(calendar.currentMonth);
  // })
  $.ajax({
    url : "/sign/list?pageSize=1000000",
    dataType : "json",
    success : function(resp){
      var datas = resp.data;
      for(var i in datas){
        var sign_data = datas[i];
        var dt_am = moment(sign_data.dt_am,"YYYY-MM-DD HH:mm:ss").format("HHmm");
        var dt_pm = moment(sign_data.dt_pm,"YYYY-MM-DD HH:mm:ss").format("HHmm");
        var duration = moment(sign_data.dt_pm,"YYYY-MM-DD HH:mm:ss") - moment(sign_data.dt_am,"YYYY-MM-DD HH:mm:ss");
        duration = duration/(1000 * 60 * 60);
        var duration_remark = parseInt(duration) + "小时" + parseInt((duration  - parseInt(duration)) * 60) + "分钟";
        sign_data.duration = duration;
        sign_data.duration_remark = duration_remark;
        if(dt_am <= sign_data.l_dt_am && dt_pm >= sign_data.l_dt_pm){
          sign_data.status = "0";
          sign_data.status_name = "正常";
        }else if(dt_am > sign_data.l_dt_am){
          sign_data.status = "1"; //迟到
          sign_data.status_name = "迟到";
        }else{
          sign_data.status = "2"; //早退
          sign_data.status_name = "早退";
        }
        sign_map[sign_data.init_date] = sign_data;
      }
      initCalendar();
    }
  })
})

// function drawCalendar(){
//   for(var i in current_days){

//   }
// }
function initCalendar(){
  var today = moment().format("YYYYMMDD");
  var config = {
    "locale": "zh" ,
    "inline" : true,
    dateFormat : "Ymd",
    defaultDate : today,
    onChange : function(selectedDates, dateStr, instance){
      if(selectedDates.length > 0 ){
        var init_date = moment(selectedDates[0]).format("YYYYMMDD");
        var sign_data = sign_map[init_date];
        if(sign_data){
          $("#dt_am").html(sign_data.dt_am);
          $("#dt_pm").html(sign_data.dt_pm);
          $("#duration_remark").html(sign_data.duration_remark);
          if(sign_data.status == "0"){
            $("#status_name").css({"color":"green","font-weight":"bold"});
          }else{
            $("#status_name").css({"color":"red","font-weight":"bold"});
          }
          $("#status_name").html(sign_data.status_name);
        }else{
          $("#dt_am").html("--");
          $("#dt_pm").html("--".dt_pm);
          $("#duration_remark").html("0小时0分钟");
          $("#status_name").css({"color":"red","font-weight":"bold"});
          $("#status_name").html("无记录");
        }
      }      
    },
    onReady : function(){
      this.setDate(new Date(),true);
    },
    onDayCreate : function(dObj, dStr, fp, dayElem){
      var date = dayElem.dateObj;
      var init_date = moment(date).format("YYYYMMDD");
      var sign_data = sign_map[init_date];
      if(sign_data){
        var dt_am = moment(sign_data.dt_am,"YYYY-MM-DD HH:mm:ss").format("HHmmss");
        var dt_pm = moment(sign_data.dt_pm,"YYYY-MM-DD HH:mm:ss").format("HHmmss");
        if(dt_am < sign_data.l_dt_am && dt_pm > sign_data.l_dt_pm){
          dayElem.className += ' greenClass ';
        }else{
          dayElem.className += ' redClass ';
        }
      }
    }
  };
  calendar = $("#calendar").flatpickr(config);
}